package com.pig4cloud.pigx.pay.handler.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bblocks.enums.SysEnum;
import com.bblocks.exception.ServiceException;
import com.pig4cloud.pigx.pay.entity.DTO.PayParamsDTO;
import com.pig4cloud.pigx.pay.entity.PayChannel;
import com.pig4cloud.pigx.pay.entity.PayTradeOrder;
import com.pig4cloud.pigx.pay.mapper.PayChannelMapper;
import com.pig4cloud.pigx.pay.mapper.PayTradeOrderMapper;
import com.pig4cloud.pigx.pay.service.PayChannelService;
import com.pig4cloud.pigx.pay.service.PayTradeOrderService;
import com.pig4cloud.pigx.pay.utils.ChannelPayApiConfigKit;
import com.pig4cloud.pigx.pay.utils.OrderStatusEnum;
import com.pig4cloud.pigx.pay.utils.PayChannelNameEnum;
import com.yungouos.pay.merge.MergePay;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lengleng
 * @date 2021/1/4
 * <p>
 * https://open.pay.yungouos.com/#/api/api/pay/merge/nativePay 服务商聚合支付模式
 */
@Slf4j
@Service("MERGE_PAY")
@RequiredArgsConstructor
public class YungouosMergePayOrderHandler extends AbstractPayOrderHandler {


	private final PayTradeOrderService payTradeOrderService;
	private final PayChannelService payChannelService;

	/**
	 * 准备支付参数
	 * @return
	 */
	@Override
	public PayChannel preparePayParams(Long tenantId) {
		PayChannel channel = payChannelService.getChannel(PayChannelNameEnum.WEIXIN_MP,tenantId);

		if (channel == null) {
			throw new IllegalArgumentException("聚合支付渠道配置为空");
		}

		return channel;
	}

	public PayTradeOrder createTradeOrder(PayParamsDTO payInfo){
		PayTradeOrder tradeOrder = this.setTradeOrderBase(payInfo);

		payTradeOrderService.saveOne(tradeOrder);
		return tradeOrder;
	}

	/**
	 * 调起渠道支付
	 * @param tradeOrder 交易订单
	 * @return obj
	 */
	@Override
	public Object collection( PayTradeOrder tradeOrder) {
		/*PayChannel channel = ChannelPayApiConfigKit.get();

		String money = NumberUtil.div(tradeOrder.getAmount(), "100", 2).toString();

		return MergePay.nativePay(String.valueOf(tradeOrder.getOrderId()), money, channel.getChannelMchId(),
				tradeOrder.getBody(), "1", TenantContextHolder.getTenantId().toString(),
				ChannelPayApiConfigKit.get().getNotifyUrl() + "/admin/notify/merge/callbak",
				ChannelPayApiConfigKit.get().getReturnUrl(), "", "", "", channel.getParam());*/
		throw new ServiceException(SysEnum.ERR_NO_REALIZE);
	}

	public Object pay(PayTradeOrder tradeOrder){
		throw new ServiceException(SysEnum.ERR_NO_REALIZE);
	}

}
